<template>
  <div>
    <!-- check-strictly 在显示复选框的情况下，是否严格的遵循父子不互相关联的做法，默认为 false	 -->
    <el-tree
      ref="tree"
      default-expand-all
      node-key="id"
      check-strictly
      show-checkbox
      :data="list"
      :props="{label:'name'}"
    />
    <el-row type="flex" justify="end">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button type="info">取消</el-button>
    </el-row>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permissions'
import { tranDateToTreeDate } from '@/utils/index'
import { getRoleById, assignPerm } from '@/api/settings'
export default {
  props: {
    roleId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: [] // 存储获取的树形数据
    }
  },
  created() {
    this.loadPermissionList()
  },
  methods: {
    // 提交
    async hSubmit() {
      try {
        // 获取用户选中的多选数据
        const permIds = this.$refs.tree.getCheckedKeys()
        console.log(permIds)
        // 发送请求
        const res = await assignPerm({ id: this.roleId, permIds: permIds })
        this.$message.success(res.message)
        // 子传父
        this.$emit('close')
      } catch (error) {
        console.log(error)
      }
    },
    // 获取权限列表
    async loadPermissionList() {
      try {
        const res = await getPermissionList()
        // console.log('getPermissionList', res)
        // 保存数据 转换成树形数据
        this.list = tranDateToTreeDate(res.data)
        const info = await getRoleById(this.roleId)
        this.$refs.tree.setCheckedKeys(info.data.permIds)
      } catch (err) {
        console.log('getPermissionList', err)
      }
    }
  }
}
</script>

<style>

</style>
